<?php

namespace App\Model\Order;

use App\Model\Model;
use App\Model\User\User;
use Hyperf\Database\Model\SoftDeletes;

/**
 * @property integer $id
 * @property integer $pid
 * @property string $order_id
 * @property string $trade_no
 * @property integer $uid
 * @property string $real_name
 * @property string $user_phone
 * @property string $user_address
 * @property string $cart_id
 * @property decimal $freight_price
 * @property integer $total_num
 * @property decimal $total_price
 * @property decimal $total_postage
 * @property decimal $pay_price
 * @property decimal $pay_postage
 * @property decimal $deduction_price
 * @property integer $coupon_id
 * @property decimal $coupon_price
 * @property boolean $paid
 * @property integer $pay_time
 * @property string $pay_type
 * @property integer $add_time
 * @property boolean $status
 * @property boolean $refund_status
 * @property boolean $refund_type
 * @property string $refund_express
 * @property string $refund_express_name
 * @property string $refund_reason_wap_img
 * @property string $refund_reason_wap_explain
 * @property integer $refund_reason_time
 * @property string $refund_reason_wap
 * @property string $refund_reason
 * @property decimal $refund_price
 * @property string $delivery_name
 * @property string $delivery_code
 * @property string $delivery_type
 * @property string $delivery_id
 * @property string $fictitious_content
 * @property integer $delivery_uid
 * @property decimal $gain_integral
 * @property decimal $use_integral
 * @property decimal $back_integral
 * @property integer $spread_uid
 * @property integer $spread_two_uid
 * @property decimal $one_brokerage
 * @property decimal $two_brokerage
 * @property string $mark
 * @property boolean $is_del
 * @property string $unique
 * @property string $remark
 * @property integer $mer_id
 * @property boolean $is_mer_check
 * @property integer $combination_id
 * @property integer $pink_id
 * @property decimal $cost
 * @property integer $seckill_id
 * @property integer $bargain_id
 * @property integer $advance_id
 * @property string $verify_code
 * @property integer $store_id
 * @property boolean $shipping_type
 * @property integer $clerk_id
 * @property boolean $is_channel
 * @property boolean $is_remind
 * @property boolean $is_system_del
 * @property string $channel_type
 * @property string $province
 * @property string $express_dump
 * @property boolean $virtual_type
 * @property string $virtual_info
 * @property integer $pay_uid
 * @property text $custom_form
 * @property integer $staff_id
 * @property integer $agent_id
 * @property integer $division_id
 * @property decimal $staff_brokerage
 * @property decimal $agent_brokerage
 * @property decimal $division_brokerage
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $delete_time
 */
class StoreOrder extends Model
{
    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'store_order';

    protected $fillable = [
        'pid',
        'order_id',
        'trade_no',
        'uid',
        'real_name',
        'user_phone',
        'user_address',
        'cart_id',
        'freight_price',
        'total_num',
        'total_price',
        'total_postage',
        'pay_price',
        'pay_postage',
        'deduction_price',
        'coupon_id',
        'coupon_price',
        'paid',
        'pay_time',
        'pay_type',
        'add_time',
        'status',
        'refund_status',
        'refund_type',
        'refund_express',
        'refund_express_name',
        'refund_reason_wap_img',
        'refund_reason_wap_explain',
        'refund_reason_time',
        'refund_reason_wap',
        'refund_reason',
        'refund_price',
        'delivery_name',
        'delivery_code',
        'delivery_type',
        'delivery_id',
        'fictitious_content',
        'delivery_uid',
        'gain_integral',
        'use_integral',
        'back_integral',
        'spread_uid',
        'spread_two_uid',
        'one_brokerage',
        'two_brokerage',
        'mark',
        'is_del',
        'unique',
        'remark',
        'mer_id',
        'is_mer_check',
        'combination_id',
        'pink_id',
        'cost',
        'seckill_id',
        'bargain_id',
        'advance_id',
        'verify_code',
        'store_id',
        'shipping_type',
        'clerk_id',
        'is_channel',
        'is_remind',
        'is_system_del',
        'channel_type',
        'province',
        'express_dump',
        'virtual_type',
        'virtual_info',
        'pay_uid',
        'custom_form',
        'staff_id',
        'agent_id',
        'division_id',
        'staff_brokerage',
        'agent_brokerage',
        'division_brokerage',
        'create_time',
        'update_time',
        'delete_time',
    ];

    protected $appends = [
        'status_format',
        'progress'
    ];


    public function StoreOrderCartInfo()
    {
        return $this->hasOne(StoreOrderCartInfo::class, 'oid', 'id');
    }

    public function getProgressAttribute()
    {
        return $this->checkAttributes(['paid', 'status', 'shipping_type', 'refund_status'], function ($paid, $status, $shipping_type, $refund_status) {
            if ($paid == 0 && $status == 0 && $refund_status == 0) {
                $progress = 1;//未支付
            } else if ($paid == 1 && $status == 0 && $refund_status == 0) {
                $progress = 2;//已支付 未发货
            } else if ($paid == 1 && $status == 4 && $refund_status == 0) {
                $progress = 8;//已支付 部分发货
            } else if ($paid == 1 && $refund_status == 1) {
                $progress = 3;//已支付 申请退款中
            } else if ($paid == 1 && $status == 1 && $refund_status == 0) {
                $progress = 4;//已支付 待收货
            } else if ($paid == 1 && $status == 2 && $refund_status == 0) {
                $progress = 5;//已支付 待评价
            } else if ($paid == 1 && $status == 3 && $refund_status == 0) {
                $progress = 6;//已支付 已完成
            } else if ($paid == 1 && $refund_status == 2) {
                $progress = 7;//已支付 已退款
            } else if ($paid == 1 && $refund_status == 3 && $status == 4) {
                $progress = 9;//拆单发货 部分申请退款
            } else if ($paid == 1 && $refund_status == 4) {
                $progress = 10;//拆单发货 已全部申请退款
            } else if ($paid == 1 && $refund_status == 3 && $status == 0) {
                $progress = 11;//拆单退款 未发货
            }
            return $progress;
        });
    }

    public function getStatusFormatAttribute()
    {
        return $this->checkAttributes(['paid', 'status', 'shipping_type', 'refund_status'], function ($paid, $status, $shipping_type, $refund_status) {
            if ($paid == 0 && $status == 0) {
                $status_name = '未支付';
            } else if ($paid == 1 && $status == 0 && $shipping_type == 1 && $refund_status == 0) {
                $status_name = '未发货';
            } else if ($paid == 1 && $status == 4 && $shipping_type == 1 && $refund_status == 0) {
                $status_name = '部分发货';
            } else if ($paid == 1 && $status == 0 && $shipping_type == 2 && $refund_status == 0) {
                $status_name = '未核销';
            } else if ($paid == 1 && $status == 1 && $shipping_type == 1 && $refund_status == 0) {
                $status_name = '待收货';
            } else if ($paid == 1 && $status == 1 && $shipping_type == 2 && $refund_status == 0) {
                $status_name = '未核销';
            } else if ($paid == 1 && $status == 2 && $refund_status == 0) {
                $status_name = '待评价';
            } else if ($paid == 1 && $status == 3 && $refund_status == 0) {
                $status_name = '已完成';
            } else if ($paid == 1 && $refund_status == 1) {
                $status_name = '申请退款中';
            } else if ($paid == 1 && $refund_status == 2) {
                $status_name = '已退款';
            } else if ($paid == 1 && $refund_status == 3) {
                $status_name = <<<HTML
<b style="color:#f124c7">部分退款</b><br/>
HTML;
            } else if ($paid == 1 && $refund_status == 4) {
                $status_name = <<<HTML
<b style="color:#f124c7">退款中</b><br/>
HTML;
            }
            return $status_name;
        }, '未知状态');
    }

    public function User()
    {
        return $this->hasOne(User::class, 'uid', 'uid');
    }
}
